﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace System.Windows.Forms
{
    /// <summary>
    /// 表示带边框的面板控件。
    /// </summary>
    /// <creator>marc</creator>
    public partial class BorderPanel : Panel
    {
        /// <summary>
        /// 表示边框颜色
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [DefaultValue(true)]
        [Description("表示边框颜色")]
        public Color BorderColor { get; set; } = Color.FromArgb(255, 79, 129, 189);

        /// <summary>
        /// 表示边框厚度
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [DefaultValue(2.2)]
        [Description("表示边框厚度")]
        public float BorderWidth { get; set; } = 2.2f;

        /// <inheritdoc cref="BorderPanel"/>
        public BorderPanel()
        {
        }

        /// <inheritdoc/>
        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            Invalidate();
        }

        /// <inheritdoc/>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            Color foreColor = ForeColor;
            Color backColor = BackColor;
            string text = Text;

            using (Brush brush = new SolidBrush(backColor))
            {
                pevent.Graphics.FillRectangle(brush, ClientRectangle);
            }

            float borderWidth = BorderWidth;
            Color borderColor = BorderColor;
            using (Pen pen = new Pen(borderColor, borderWidth))
            {
                pevent.Graphics.DrawRectangle(pen, new Rectangle(0, 0, Width - 1, Height - 1));
            }
            RectangleF rectangle = new RectangleF(borderWidth, borderWidth, Width - 1 - borderWidth * 2, Height - 1 - borderWidth * 2);
            using (Brush brush = new SolidBrush(foreColor))
            {
                StringFormat stringFormat = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };
                pevent.Graphics.DrawString(text, Font, brush, rectangle, stringFormat);
            }
        }

        /// <inheritdoc/>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }
    }
}
